package id.ac.amikom.amikomsocial;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.MotionEvent;
import android.view.Window;
import android.view.WindowManager;

public class AdemSplashActivity extends Activity {

	protected int _splashTime = 1800;
	private Thread splashTread;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {

		requestWindowFeature(Window.FEATURE_NO_TITLE);

		super.onCreate(savedInstanceState);
		setContentView(R.layout.adem_splash);

		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);

		final AdemSplashActivity sPlashScreen = this;

		SharedPreferences prefs = PreferenceManager
				.getDefaultSharedPreferences(this);

		Intent myIntent = new Intent(AdemSplashActivity.this,
				AdemServiceRefresh.class);
		if (prefs.getBoolean("id_auto", false)) {						
			stopService(myIntent);
			startService(myIntent);
		} else {
			stopService(myIntent);
		}

		// thread for displaying the SplashScreen
		splashTread = new Thread() {
			@Override
			public void run() {
				try {
					synchronized (this) {
						wait(_splashTime);
					}

				} catch (InterruptedException e) {
				} finally {
					finish();

					Intent i = new Intent();
					i.setClass(sPlashScreen, AdemTabActivity.class);
					i.putExtra("url", "");
					startActivity(i);
					
				}
			}
		};

		splashTread.start();
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		if (event.getAction() == MotionEvent.ACTION_DOWN) {
			synchronized (splashTread) {
				splashTread.notifyAll();
			}
		}
		return true;
	}

}
